package com.jp.knowledge.my.activity;

import android.content.Intent;
import android.view.View;

import com.google.gson.JsonObject;
import com.jiayen.util.ToasUtil;
import com.jp.knowledge.R;
import com.jp.knowledge.comm.SlidingActivity;
import com.jp.knowledge.http.HttpHelper;
import com.jp.knowledge.model.IModel;
import com.jp.knowledge.my.model.HelpCatePageModel;
import com.jp.knowledge.util.ResultSubscriber;
import com.jp.knowledge.webUtil.X5WebView;

import org.xutils.view.annotation.ViewInject;

/**
 * Created by 528787067@qq.com on 2017/6/21.
 */

public class HelpCatePageActivity extends SlidingActivity {

	@ViewInject(R.id.showLogView)
	private X5WebView helpShowView;

	private String id;

	@Override
	protected int getResourceId() {
		return R.layout.activity_net_log;
	}

	@Override
	protected void init() {
		id = getIntent().getStringExtra("pageId");
		String title = getIntent().getStringExtra("title");
		topName.setText(title == null ? "帮助" : title);
		rightIcon.setVisibility(View.GONE);
		leftIcon.setImageResource(R.mipmap.arrow_white_left);
		leftIcon.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				onBackPressed();
			}
		});
		getDate();
	}

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		id = getIntent().getStringExtra("pageId");
		getDate();
	}

	@Override
	protected void onDestroy() {
		helpShowView.destroy();
		super.onDestroy();
	}

	private void getDate(){
		JsonObject param = new JsonObject();
		param.addProperty("id", id);
		HttpHelper.getInstance(mContext).userHelpCatePage(param, 0x01, new ResultSubscriber.OnResultListener() {
			@Override
			public void onStart(int requestType) {
				loading.show();
			}
			@Override
			public void onCompleted(int requestType) {
				loading.cancel();
			}
			@Override
			public void onError(int requestType) {
				onCompleted(requestType);
			}
			@Override
			public void onNext(IModel data, int requestType) {
				if(data.getErrcode() != 0){
					ToasUtil.toast(mContext, data.getMessage());
					onError(requestType);
				} else{
					HelpCatePageModel helpCatePageModel = data.getEntity(HelpCatePageModel.class);
					if(helpCatePageModel != null){
						topName.setText(helpCatePageModel.getTitle());
						helpShowView.loadData(helpCatePageModel.getContent(), "text/html; charset=utf-8", null);
					}
				}
			}
		});
	}
}
